package com.cre_drt;

import java.text.SimpleDateFormat;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.PhoneNumberFormattingTextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import com.att.intern.webservice.Incident;
import com.att.intern.webservice.Webservice;
import com.att.intern.webservice.Webservice.GeolocInvalidException;
import com.att.intern.webservice.Webservice.TokenInvalidException;

@SuppressLint("SimpleDateFormat")
public class UpdateDamageAssessmentScreen extends ActivityTimeout {

	EditText recordIDEdit;
	EditText glcEdit;
	EditText stateEdit;
	EditText pmEdit;
	EditText buildingNameEdit;
	EditText addressEdit;
	Spinner eventYearDropdown;
	EditText requestorUserIDEdit;
	EditText contactNumberEdit;
	EditText initialDateReportEdit;
	EditText eventYearEditTextView;
	EditText eventNameEditTextView;
	Spinner eventTypeDropdown;
	Spinner eventNameDropdown;
	Spinner propertyTypeDropdown;
	EditText workRequestNumber;
	Spinner buildingStatusDropdown;
	Spinner creLeadDropdown;
	EditText capitalEdit;
	EditText expenseEdit;
	EditText assessmentEdit;
	EditText statusEdit;
	Spinner reportOpenClosedSpinner;
	EditText completionDateEdit;
	EditText notesEdit;

	String commercialPower;
	String onGenerator;
	String damaged;
	String mobilityCo;
	String unoccupiable;

	String eventYear = "";
	String eventType = "";
	String eventName = "";
	
	int recNum;

	HashMap<String, Integer> eventIds = new HashMap<String, Integer>() {
		private static final long serialVersionUID = 1L;
		{
			put("default", R.array.blankDropdown);
			put("hurricaneDropdown2010", R.array.hurricaneDropdown2010);
			put("hurricaneDropdown2011", R.array.hurricaneDropdown2011);
			put("hurricaneDropdown2012", R.array.hurricaneDropdown2012);
			put("hurricaneDropdown2013", R.array.hurricaneDropdown2013);
			put("earthquakeDropdown2010", R.array.earthquakeDropdown2010);
			put("earthquakeDropdown2011", R.array.earthquakeDropdown2011);
			put("earthquakeDropdown2012", R.array.earthquakeDropdown2012);
			put("earthquakeDropdown2013", R.array.earthquakeDropdown2013);
			put("floodDropdown2010", R.array.floodDropdown2010);
			put("floodDropdown2011", R.array.floodDropdown2011);
			put("floodDropdown2012", R.array.floodDropdown2012);
			put("floodDropdown2013", R.array.floodDropdown2013);
			put("ice stormDropdown2010", R.array.iceStormDropdown2010);
			put("ice stormDropdown2011", R.array.iceStormDropdown2011);
			put("ice stormDropdown2012", R.array.iceStormDropdown2012);
			put("ice stormDropdown2013", R.array.iceStormDropdown2013);
			put("snow stormDropdown2010", R.array.snowStormDropdown2010);
			put("snow stormDropdown2011", R.array.snowStormDropdown2011);
			put("snow stormDropdown2012", R.array.snowStormDropdown2012);
			put("snow stormDropdown2013", R.array.snowStormDropdown2013);
			put("tornadoDropdown2010", R.array.tornadoDropdown2010);
			put("tornadoDropdown2012", R.array.tornadoDropdown2011);
			put("tornadoDropdown2012", R.array.tornadoDropdown2012);
			put("tornadoDropdown2013", R.array.tornadoDropdown2013);
			put("wildfireDropdown2010", R.array.wildfireDropdown2010);
			put("wildfireDropdown2011", R.array.wildfireDropdown2011);
			put("wildfireDropdown2012", R.array.wildfireDropdown2012);
			put("wildfireDropdown2013", R.array.wildfireDropdown2013);
		}
	};

	String reqFields;
	boolean reqCheck;
	String geoloc;
	String geolocResult;
	String[] parsedResult;

	HashMap<String, String> sessionInfo;
	Webservice UpdateAssessmentWebservice;
	int incidentRecNum;
	Incident incident;

	SessionManager session;
	SimpleDateFormat formatter;
	ProgressDialog progressDialog;
	ProgressDialog openingIncidentProgressDialog;
	InputMethodManager imm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_damage_assessment_screen);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Update Damage Assesment");
		actionBar.setSubtitle("Enter Information");

		session = new SessionManager(getApplicationContext());
		sessionInfo = session.getSessionDetails();
		formatter = new SimpleDateFormat("MM/dd/yyyy");

		reqFields = "The following fields are required:\n";
		reqCheck = true;
		geoloc = getIntent().getExtras().getString("geoloc");
		geolocResult = getIntent().getExtras().getString("geolocResult");

		parsedResult = geolocResult.split("~");

		UpdateAssessmentWebservice = new Webservice(getApplicationContext());

		recNum = getIntent().getIntExtra("incident record num", 0);
		
		progressDialog = new ProgressDialog(this);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage("Updating incident...");
		progressDialog.setCancelable(false);
		
		openingIncidentProgressDialog = new ProgressDialog(this);
		openingIncidentProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		openingIncidentProgressDialog.setMessage("Retrieving Incident Details...");
		openingIncidentProgressDialog.setCancelable(false);
		
		openingIncidentProgressDialog.show();
		new updateIncidentAsyncTaskClass().execute();		

		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.incident_search_screen, menu);
		return true;
	}
	
	private class updateIncidentAsyncTaskClass extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			try {
				incident = UpdateAssessmentWebservice.incidByRecNum(sessionInfo.get(session.KEY_SESSION_TOKEN_ID), recNum);
			} catch (TokenInvalidException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			openingIncidentProgressDialog.dismiss();
			setUpForm();
		}
	}

	@SuppressLint("SimpleDateFormat")
	private void setUpForm() {
		recordIDEdit = (EditText) findViewById(R.id.recordIDEdit);
		recordIDEdit.setKeyListener(null);
		recordIDEdit.setText(String.valueOf(incident.getRecNumber()));

		glcEdit = (EditText) findViewById(R.id.glcEdit);
		glcEdit.setKeyListener(null);
		glcEdit.setText(String.valueOf(incident.getGeoLoc()));

		stateEdit = (EditText) findViewById(R.id.stateEdit);
		stateEdit.setKeyListener(null);
		stateEdit.setText(String.valueOf(incident.getState()));

		pmEdit = (EditText) findViewById(R.id.pmEdit);
		pmEdit.setKeyListener(null);
		pmEdit.setText(String.valueOf(incident.getPMAttuid()));

		buildingNameEdit = (EditText) findViewById(R.id.buildingNameEdit);
		buildingNameEdit.setKeyListener(null);
		buildingNameEdit.setText(String.valueOf(incident.getBuildingName()));

		addressEdit = (EditText) findViewById(R.id.addressEdit);
		addressEdit.setKeyListener(null);
		addressEdit.setText(String.valueOf(incident.getBuildingAddress()));

		requestorUserIDEdit = (EditText) findViewById(R.id.requestorUserIDEdit);
		requestorUserIDEdit.setText(String.valueOf(incident.getReqATTUID()));

		contactNumberEdit = (EditText) findViewById(R.id.contactNumberEdit);
		contactNumberEdit.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
		contactNumberEdit.setText(String.valueOf(incident.getContactPhone()));

		initialDateReportEdit = (EditText) findViewById(R.id.initialReportDateEdit);
		initialDateReportEdit.setText(String.valueOf(incident.getInitialRptDate()));

		eventYearEditTextView = (EditText) findViewById(R.id.eventYearEditTextView);
		eventYearEditTextView.setText(String.valueOf(incident.getIncidentYear()));

		eventNameEditTextView = (EditText) findViewById(R.id.eventNameEditTextView);
		eventNameEditTextView.setText(String.valueOf(incident.getEventName()));

		propertyTypeDropdown = (Spinner) findViewById(R.id.propertyTypeDropdown);
		propertyTypeDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
				String propertyType = propertyTypeDropdown.getSelectedItem().toString();
				CheckBox commercialPowerCheckBox = (CheckBox) findViewById(R.id.commercialPowerCheckBox);
				CheckBox onGeneratorCheckBox = (CheckBox) findViewById(R.id.onGeneratorCheckBox);
				if (propertyType.equals("CO")) {
					commercialPowerCheckBox.setChecked(false);
					onGeneratorCheckBox.setChecked(false);
					commercialPowerCheckBox.setTextColor(getResources().getColor(android.R.color.darker_gray));
					onGeneratorCheckBox.setTextColor(getResources().getColor(android.R.color.darker_gray));

					commercialPowerCheckBox.setFocusable(false);
					commercialPowerCheckBox.setClickable(false);
					onGeneratorCheckBox.setFocusable(false);
					onGeneratorCheckBox.setClickable(false);
				} else {
					commercialPowerCheckBox.setTextColor(getResources().getColor(android.R.color.black));
					onGeneratorCheckBox.setTextColor(getResources().getColor(android.R.color.black));

					commercialPowerCheckBox.setFocusable(true);
					commercialPowerCheckBox.setClickable(true);
					onGeneratorCheckBox.setFocusable(true);
					onGeneratorCheckBox.setClickable(true);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		String propertyType = String.valueOf(incident.getBuildingType());

		if (propertyType.equals("ADM")) {
			propertyTypeDropdown.setSelection(0);
		} else if (propertyType.equals("CEV")) {
			propertyTypeDropdown.setSelection(1);
		} else if (propertyType.equals("CO")) {
			propertyTypeDropdown.setSelection(2);
		} else if (propertyType.equals("DAT")) {
			propertyTypeDropdown.setSelection(3);
		} else if (propertyType.equals("HUT")) {
			propertyTypeDropdown.setSelection(4);
		} else if (propertyType.equals("LAB")) {
			propertyTypeDropdown.setSelection(5);
		} else if (propertyType.equals("LAND")) {
			propertyTypeDropdown.setSelection(6);
		} else if (propertyType.equals("OTHER")) {
			propertyTypeDropdown.setSelection(7);
		} else if (propertyType.equals("RET")) {
			propertyTypeDropdown.setSelection(8);
		} else if (propertyType.equals("STO")) {
			propertyTypeDropdown.setSelection(9);
		} else if (propertyType.equals("TWR")) {
			propertyTypeDropdown.setSelection(10);
		} else if (propertyType.equals("WAR")) {
			propertyTypeDropdown.setSelection(11);
		} else if (propertyType.equals("WCR")) {
			propertyTypeDropdown.setSelection(12);
		}

		workRequestNumber = (EditText) findViewById(R.id.workRequestNumberEdit);
		workRequestNumber.setText(String.valueOf(incident.getWorkReqNumber()));

		buildingStatusDropdown = (Spinner) findViewById(R.id.buildingStatusDropdown);
		String buildingStatus = String.valueOf(incident.getBuildingStatus());

		if (buildingStatus.equals("OPEN")) {
			buildingStatusDropdown.setSelection(0);
		} else if (buildingStatus.equals("CLOSED")) {
			buildingStatusDropdown.setSelection(1);
		} else if (buildingStatus.equals("DELAYED OPENING")) {
			buildingStatusDropdown.setSelection(2);
		} else if (buildingStatus.equals("RE-OPENED")) {
			buildingStatusDropdown.setSelection(3);
		}

		creLeadDropdown = (Spinner) findViewById(R.id.creLeadDropdown);
		String CRELead = String.valueOf(incident.getCreLead());

		if (CRELead.equals("PM")) {
			creLeadDropdown.setSelection(0);
		} else if (CRELead.equals("D&C")) {
			creLeadDropdown.setSelection(1);
		}

		capitalEdit = (EditText) findViewById(R.id.capitalEdit);
		capitalEdit.setText(String.valueOf(incident.getEstCapCost()));

		expenseEdit = (EditText) findViewById(R.id.expenseEdit);
		expenseEdit.setText(String.valueOf(incident.getEstExpenseCost()));

		assessmentEdit = (EditText) findViewById(R.id.assessmentEdit);
		assessmentEdit.setText(String.valueOf(incident.getAssessNotes()));

		statusEdit = (EditText) findViewById(R.id.statusEdit);
		statusEdit.setText(String.valueOf(incident.getStatusNotes()));

		reportOpenClosedSpinner = (Spinner) findViewById(R.id.reportOpenCloseDropdown);
		String reportOpenClosed = String.valueOf(incident.getIncidentStatus());

		if (reportOpenClosed.equals("Open")) {
			reportOpenClosedSpinner.setSelection(0);
		} else if (reportOpenClosed.equals("Closed")) {
			reportOpenClosedSpinner.setSelection(1);
		}

		completionDateEdit = (EditText) findViewById(R.id.completionDateEdit);
		completionDateEdit.setText(String.valueOf(incident.getCompltnDate()));

		notesEdit = (EditText) findViewById(R.id.notesEdit);
		notesEdit.setText(String.valueOf(incident.getIncidentNotes()));

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getComPowerIndicator()).equals("Y")) {
			CheckBox comPowerCheckBox = (CheckBox) findViewById(R.id.commercialPowerCheckBox);
			comPowerCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getComPowerIndicator()).equals("N")) {
			CheckBox comPowerCheckBox = (CheckBox) findViewById(R.id.commercialPowerCheckBox);
			comPowerCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getOnGeneratorIndicator()).equals("Y")) {
			CheckBox onGenCheckBox = (CheckBox) findViewById(R.id.onGeneratorCheckBox);
			onGenCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getOnGeneratorIndicator()).equals("N")) {
			CheckBox onGenCheckBox = (CheckBox) findViewById(R.id.onGeneratorCheckBox);
			onGenCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getDamageIndicator()).equals("Y")) {
			CheckBox damagedCheckBox = (CheckBox) findViewById(R.id.damagedCheckBox);
			damagedCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getDamageIndicator()).equals("N")) {
			CheckBox damagedCheckBox = (CheckBox) findViewById(R.id.damagedCheckBox);
			damagedCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getMobCOIndicator()).equals("Y")) {
			CheckBox mobilityCOCheckBox = (CheckBox) findViewById(R.id.mobilityCoCheckBox);
			mobilityCOCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getMobCOIndicator()).equals("N")) {
			CheckBox mobilityCOCheckBox = (CheckBox) findViewById(R.id.mobilityCoCheckBox);
			mobilityCOCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getUnOccupiableIndicator()).equals("Y")) {
			CheckBox unOccupiableCheckBox = (CheckBox) findViewById(R.id.unoccupiableCheckBox);
			unOccupiableCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getUnOccupiableIndicator()).equals("N")) {
			CheckBox unOccupiableCheckBox = (CheckBox) findViewById(R.id.unoccupiableCheckBox);
			unOccupiableCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getElecIssueIndicator()).equals("Y")) {
			CheckBox elecIssueCheckBox = (CheckBox) findViewById(R.id.electricalIssueCheckBox);
			elecIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getElecIssueIndicator()).equals("N")) {
			CheckBox elecIssueCheckBox = (CheckBox) findViewById(R.id.electricalIssueCheckBox);
			elecIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getElecIssueClsdIndicator()).equals("Y")) {
			CheckBox elecIssueClsdCheckBox = (CheckBox) findViewById(R.id.electricalClosedCheckBox);
			elecIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getElecIssueClsdIndicator()).equals("N")) {
			CheckBox elecIssueClsdCheckBox = (CheckBox) findViewById(R.id.electricalClosedCheckBox);
			elecIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getEnvIssueIndicator()).equals("Y")) {
			CheckBox envIssueCheckBox = (CheckBox) findViewById(R.id.environmentalIssueCheckBox);
			envIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getEnvIssueIndicator()).equals("N")) {
			CheckBox envIssueCheckBox = (CheckBox) findViewById(R.id.environmentalIssueCheckBox);
			envIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getEnvIssueClsdIndicator()).equals("Y")) {
			CheckBox envIssueClsdCheckBox = (CheckBox) findViewById(R.id.environmentalClosedCheckBox);
			envIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getEnvIssueClsdIndicator()).equals("N")) {
			CheckBox envIssueClsdCheckBox = (CheckBox) findViewById(R.id.environmentalClosedCheckBox);
			envIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getFenceGateIssueIndicator()).equals("Y")) {
			CheckBox fenceGatesIssueCheckBox = (CheckBox) findViewById(R.id.fenceGatesIssueCheckBox);
			fenceGatesIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getFenceGateIssueIndicator()).equals("N")) {
			CheckBox fenceGatesIssueCheckBox = (CheckBox) findViewById(R.id.fenceGatesIssueCheckBox);
			fenceGatesIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getFenceGateIssueClsdIndicator()).equals("Y")) {
			CheckBox fenceGatesIssueClsdCheckBox = (CheckBox) findViewById(R.id.fenceGatesClosedCheckBox);
			fenceGatesIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getFenceGateIssueClsdIndicator()).equals("N")) {
			CheckBox fenceGatesIssueClsdCheckBox = (CheckBox) findViewById(R.id.fenceGatesClosedCheckBox);
			fenceGatesIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getGenIssueIndicator()).equals("Y")) {
			CheckBox genIssueCheckBox = (CheckBox) findViewById(R.id.generatorIssueCheckBox);
			genIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getGenIssueIndicator()).equals("N")) {
			CheckBox genIssueCheckBox = (CheckBox) findViewById(R.id.generatorIssueCheckBox);
			genIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getGenIssueClsdIndicator()).equals("Y")) {
			CheckBox genIssueClsdCheckBox = (CheckBox) findViewById(R.id.generatorClosedCheckBox);
			genIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getGenIssueClsdIndicator()).equals("N")) {
			CheckBox genIssueClsdCheckBox = (CheckBox) findViewById(R.id.generatorClosedCheckBox);
			genIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getWaterIssueIndicator()).equals("Y")) {
			CheckBox waterIssueCheckBox = (CheckBox) findViewById(R.id.waterIssueCheckBox);
			waterIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getWaterIssueIndicator()).equals("N")) {
			CheckBox waterIssueCheckBox = (CheckBox) findViewById(R.id.waterIssueCheckBox);
			waterIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getWaterIssueClsdIndicator()).equals("Y")) {
			CheckBox waterIssueClsdCheckBox = (CheckBox) findViewById(R.id.waterClosedCheckBox);
			waterIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getWaterIssueClsdIndicator()).equals("N")) {
			CheckBox waterIssueClsdCheckBox = (CheckBox) findViewById(R.id.waterClosedCheckBox);
			waterIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getGroundsIssueIndicator()).equals("Y")) {
			CheckBox groundsIssueCheckBox = (CheckBox) findViewById(R.id.groundsIssueCheckBox);
			groundsIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getGroundsIssueIndicator()).equals("N")) {
			CheckBox groundsIssueCheckBox = (CheckBox) findViewById(R.id.groundsIssueCheckBox);
			groundsIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getGroundsIssueClsdIndicator()).equals("Y")) {
			CheckBox groundsIssueClsdCheckBox = (CheckBox) findViewById(R.id.groundsClosedCheckBox);
			groundsIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getGroundsIssueClsdIndicator()).equals("N")) {
			CheckBox groundsIssueClsdCheckBox = (CheckBox) findViewById(R.id.groundsClosedCheckBox);
			groundsIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getMechIssueIndicator()).equals("Y")) {
			CheckBox mechIssueCheckBox = (CheckBox) findViewById(R.id.mechanicalIssueCheckBox);
			mechIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getMechIssueIndicator()).equals("N")) {
			CheckBox mechIssueCheckBox = (CheckBox) findViewById(R.id.mechanicalIssueCheckBox);
			mechIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getMechIssueClsdIndicator()).equals("Y")) {
			CheckBox mechIssueClsdCheckBox = (CheckBox) findViewById(R.id.mechanicalClosedCheckBox);
			mechIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getMechIssueClsdIndicator()).equals("N")) {
			CheckBox mechIssueClsdCheckBox = (CheckBox) findViewById(R.id.mechanicalClosedCheckBox);
			mechIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getOtherIssueIndicator()).equals("Y")) {
			CheckBox otherIssueCheckBox = (CheckBox) findViewById(R.id.otherIssueCheckBox);
			otherIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getOtherIssueIndicator()).equals("N")) {
			CheckBox otherIssueCheckBox = (CheckBox) findViewById(R.id.otherIssueCheckBox);
			otherIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getOtherIssueClsdIndicator()).equals("Y")) {
			CheckBox otherIssueClsdCheckBox = (CheckBox) findViewById(R.id.otherClosedCheckBox);
			otherIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getOtherIssueClsdIndicator()).equals("N")) {
			CheckBox otherIssueClsdCheckBox = (CheckBox) findViewById(R.id.otherClosedCheckBox);
			otherIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getPlumbIssueIndicator()).equals("Y")) {
			CheckBox plumbIssueCheckBox = (CheckBox) findViewById(R.id.plumbingIssueCheckBox);
			plumbIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getPlumbIssueIndicator()).equals("N")) {
			CheckBox plumbIssueCheckBox = (CheckBox) findViewById(R.id.plumbingIssueCheckBox);
			plumbIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getPlumbIssueClsdIndicator()).equals("Y")) {
			CheckBox plumbIssueClsdCheckBox = (CheckBox) findViewById(R.id.plumbingClosedCheckBox);
			plumbIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getPlumbIssueClsdIndicator()).equals("N")) {
			CheckBox plumbIssueClsdCheckBox = (CheckBox) findViewById(R.id.plumbingClosedCheckBox);
			plumbIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getRoofsIssueIndicator()).equals("Y")) {
			CheckBox roofsIssueCheckBox = (CheckBox) findViewById(R.id.roofsIssueCheckBox);
			roofsIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getRoofsIssueIndicator()).equals("N")) {
			CheckBox roofsIssueCheckBox = (CheckBox) findViewById(R.id.roofsIssueCheckBox);
			roofsIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getRoofsIssueClsdIndicator()).equals("Y")) {
			CheckBox roofsIssueClsdCheckBox = (CheckBox) findViewById(R.id.roofsClosedCheckBox);
			roofsIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getRoofsIssueClsdIndicator()).equals("N")) {
			CheckBox roofsIssueClsdCheckBox = (CheckBox) findViewById(R.id.roofsClosedCheckBox);
			roofsIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getSafetyIssueIndicator()).equals("Y")) {
			CheckBox safetyIssueCheckBox = (CheckBox) findViewById(R.id.safetyIssueCheckBox);
			safetyIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getSafetyIssueIndicator()).equals("N")) {
			CheckBox safetyIssueCheckBox = (CheckBox) findViewById(R.id.safetyIssueCheckBox);
			safetyIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getSafetyIssueClsdIndicator()).equals("Y")) {
			CheckBox safetyIssueClsdCheckBox = (CheckBox) findViewById(R.id.safetyClosedCheckBox);
			safetyIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getSafetyIssueClsdIndicator()).equals("N")) {
			CheckBox safetyIssueClsdCheckBox = (CheckBox) findViewById(R.id.safetyClosedCheckBox);
			safetyIssueClsdCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getStructIssueIndicator()).equals("Y")) {
			CheckBox structIssueCheckBox = (CheckBox) findViewById(R.id.structuralIssueCheckBox);
			structIssueCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getStructIssueIndicator()).equals("N")) {
			CheckBox structIssueCheckBox = (CheckBox) findViewById(R.id.structuralIssueCheckBox);
			structIssueCheckBox.setChecked(false);
		}

		// //////////////////////////////////////////////////////////////////////////////////////

		if (String.valueOf(incident.getStructIssueClsdIndicator()).equals("Y")) {
			CheckBox structIssueClsdCheckBox = (CheckBox) findViewById(R.id.structuralClosedCheckBox);
			structIssueClsdCheckBox.setChecked(true);
		} else if (String.valueOf(incident.getStructIssueClsdIndicator()).equals("N")) {
			CheckBox structIssueClsdCheckBox = (CheckBox) findViewById(R.id.structuralClosedCheckBox);
			structIssueClsdCheckBox.setChecked(false);
		}
	}

	public void onCheckboxClicked(View view) {
		boolean checked = ((CheckBox) view).isChecked();

		switch (view.getId()) {
		// Non-CO Facilities ONLY:
		case R.id.commercialPowerCheckBox:
			if (checked) {
				incident.setComPowerIndicator("Y");
			} else {
				incident.setComPowerIndicator("N");
			}
			break;
		case R.id.onGeneratorCheckBox:
			if (checked)
				incident.setOnGeneratorIndicator("Y");
			else
				incident.setOnGeneratorIndicator("N");
			break;

		// Any Facility
		case R.id.damagedCheckBox:
			if (checked)
				incident.setDamageIndicator("Y");
			else
				incident.setDamageIndicator("N");
			break;
		case R.id.mobilityCoCheckBox:
			if (checked)
				incident.setMobCOIndicator("Y");
			else
				incident.setMobCOIndicator("N");
			break;
		case R.id.unoccupiableCheckBox:
			if (checked)
				incident.setUnOccupiableIndicator("Y");
			else
				incident.setUnOccupiableIndicator("N");
			break;

		// Categories
		case R.id.electricalIssueCheckBox:
			if (checked)
				incident.setElecIssueIndicator("Y");
			else
				incident.setElecIssueIndicator("N");
			break;
		case R.id.electricalClosedCheckBox:
			if (checked)
				incident.setElecIssueClsdIndicator("Y");
			else
				incident.setElecIssueClsdIndicator("N");
			break;
		case R.id.environmentalIssueCheckBox:
			if (checked) {
				incident.setEnvIssueIndicator("Y");
			} else {
				incident.setEnvIssueIndicator("N");
			}
			break;
		case R.id.environmentalClosedCheckBox:
			if (checked) {
				incident.setEnvIssueClsdIndicator("Y");
			} else {
				incident.setEnvIssueClsdIndicator("N");
			}
			break;
		case R.id.fenceGatesIssueCheckBox:
			if (checked) {
				incident.setFenceGateIssueIndicator("Y");
			} else {
				incident.setFenceGateIssueIndicator("N");
			}
			break;
		case R.id.fenceGatesClosedCheckBox:
			if (checked) {
				incident.setFenceGateIssueClsdIndicator("Y");
			} else {
				incident.setFenceGateIssueClsdIndicator("N");
			}
			break;
		case R.id.generatorIssueCheckBox:
			if (checked)
				incident.setGenIssueIndicator("Y");
			else
				incident.setGenIssueIndicator("N");
			break;
		case R.id.generatorClosedCheckBox:
			if (checked)
				incident.setGenIssueClsdIndicator("Y");
			else
				incident.setGenIssueClsdIndicator("N");
			break;
		case R.id.waterIssueCheckBox:
			if (checked)
				incident.setWaterIssueIndicator("Y");
			else
				incident.setWaterIssueIndicator("N");
			break;
		case R.id.waterClosedCheckBox:
			if (checked)
				incident.setWaterIssueClsdIndicator("Y");
			else
				incident.setWaterIssueClsdIndicator("N");
			break;
		case R.id.groundsIssueCheckBox:
			if (checked)
				incident.setGroundsIssueIndicator("Y");
			else
				incident.setGroundsIssueIndicator("N");
			break;
		case R.id.groundsClosedCheckBox:
			if (checked)
				incident.setGroundsIssueClsdIndicator("Y");
			else
				incident.setGroundsIssueClsdIndicator("N");
			break;
		case R.id.mechanicalIssueCheckBox:
			if (checked)
				incident.setMechIssueIndicator("Y");
			else
				incident.setMechIssueIndicator("N");
			break;
		case R.id.mechanicalClosedCheckBox:
			if (checked)
				incident.setMechIssueClsdIndicator("Y");
			else
				incident.setMechIssueClsdIndicator("N");
			break;
		case R.id.otherIssueCheckBox:
			if (checked)
				incident.setOtherIssueIndicator("Y");
			else
				incident.setOtherIssueIndicator("N");
			break;
		case R.id.otherClosedCheckBox:
			if (checked)
				incident.setOtherIssueClsdIndicator("Y");
			else
				incident.setOtherIssueClsdIndicator("N");
			break;
		case R.id.plumbingIssueCheckBox:
			if (checked)
				incident.setPlumbIssueIndicator("Y");
			else
				incident.setPlumbIssueIndicator("N");
			break;
		case R.id.plumbingClosedCheckBox:
			if (checked)
				incident.setPlumbIssueClsdIndicator("Y");
			else
				incident.setPlumbIssueClsdIndicator("N");
			break;
		case R.id.roofsIssueCheckBox:
			if (checked)
				incident.setRoofsIssueIndicator("Y");
			else
				incident.setRoofsIssueIndicator("N");
			break;
		case R.id.roofsClosedCheckBox:
			if (checked)
				incident.setRoofsIssueClsdIndicator("Y");
			else
				incident.setRoofsIssueClsdIndicator("N");
			break;
		case R.id.safetyIssueCheckBox:
			if (checked)
				incident.setSafetyIssueIndicator("Y");
			else
				incident.setSafetyIssueIndicator("N");
			break;
		case R.id.safetyClosedCheckBox:
			if (checked)
				incident.setSafetyIssueClsdIndicator("Y");
			else
				incident.setSafetyIssueClsdIndicator("N");
			break;
		case R.id.structuralIssueCheckBox:
			if (checked)
				incident.setStructIssueIndicator("Y");
			else
				incident.setStructIssueIndicator("N");
			break;
		case R.id.structuralClosedCheckBox:
			if (checked)
				incident.setStructIssueClsdIndicator("Y");
			else
				incident.setStructIssueClsdIndicator("N");
			break;

		}// end switch
	}

	private void recordSpinnerSelection() {
		propertyTypeDropdown = (Spinner) findViewById(R.id.propertyTypeDropdown);
		incident.setBuildingType(propertyTypeDropdown.getSelectedItem().toString());

		buildingStatusDropdown = (Spinner) findViewById(R.id.buildingStatusDropdown);
		incident.setBuildingStatus(buildingStatusDropdown.getSelectedItem().toString());

		creLeadDropdown = (Spinner) findViewById(R.id.creLeadDropdown);
		incident.setCreLead(creLeadDropdown.getSelectedItem().toString());

		reportOpenClosedSpinner = (Spinner) findViewById(R.id.reportOpenCloseDropdown);
		incident.setIncidentStatus(reportOpenClosedSpinner.getSelectedItem().toString());
	}

	private void readEditTextFields() { // add statement to grab the text of the
										// event details edit texts (2 of them)
		if (contactNumberEdit.getText().toString().equals("")) {
			reqFields = reqFields + " -Contact Number\n";
			reqCheck = false;
		} else
			incident.setContactPhone(contactNumberEdit.getText().toString().replaceAll("\\D+", ""));
		if (!capitalEdit.getText().toString().equals(""))
			incident.setEstCapCost(Integer.parseInt(capitalEdit.getText().toString()));
		if (!expenseEdit.getText().toString().equals(""))
			incident.setEstExpenseCost(Integer.parseInt(expenseEdit.getText().toString()));

		incident.setAssessNotes(assessmentEdit.getText().toString());
		incident.setStatusNotes(statusEdit.getText().toString());
		incident.setIncidentNotes(notesEdit.getText().toString());

		if (initialDateReportEdit.getText().toString().equals("")) {
			reqFields = reqFields + " -Inital Report Date Date\n";
			reqCheck = false;
		} else
			incident.setInitialRptDate(initialDateReportEdit.getText().toString());
		if (completionDateEdit.getText().toString().equals("")) {
			reqFields = reqFields + " -Completion Date\n";
			reqCheck = false;
		} else
			incident.setCompltnDate(completionDateEdit.getText().toString());
		incident.setWorkReqNumber(workRequestNumber.getText().toString());

		incident.setIncidentYear(Integer.valueOf(eventYearEditTextView.getText().toString()));
		incident.setEventName(eventNameEditTextView.getText().toString());
	}

	public void startWebservice(View view) {
		imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
		progressDialog.show();
		new AddIncidentAsyncTaskClass().execute();
	}

	private class AddIncidentAsyncTaskClass extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			updateAssessment();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			progressDialog.dismiss();
			if (!reqCheck) {
				DialogBox failWindow = new DialogBox(reqFields, false, getApplicationContext());
				failWindow.show(getSupportFragmentManager(), "RequiredFieldsNotFilled");
				reqFields = "The following fields are required:\n";
				reqCheck = true;
			} else {
				DialogBox successWindow = new DialogBox("Incident updated successfully!", true, getApplicationContext());
				successWindow.show(getSupportFragmentManager(), "SuccessFragment");
			}
		}
	}

	public void updateAssessment() {
		readEditTextFields();
		recordSpinnerSelection();
		try {
			if (reqCheck) {
				UpdateAssessmentWebservice.updateIncidentbyRecnum(sessionInfo.get(session.KEY_SESSION_TOKEN_ID), incident);
			}
		} catch (TokenInvalidException e) {
			e.printStackTrace();
		} catch (GeolocInvalidException e) {
			e.printStackTrace();
		}
	}

	public void showEventDetailDialogBox(View view) {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.custom_dialog_box);
		dialog.setTitle("Please Select Event Details.");

		// pull these from existing event to populate them initially
		// Event Name is driven by Event Year
		eventYearDropdown = (Spinner) dialog.findViewById(R.id.eventYearDropdown);
		eventNameDropdown = (Spinner) dialog.findViewById(R.id.eventNameDropdown);
		eventTypeDropdown = (Spinner) dialog.findViewById(R.id.eventTypeDropdown);

		String[] eventType = getResources().getStringArray(R.array.eventDropdown);
		ArrayAdapter<String> eventTypeAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_style, eventType);
		System.out.println("got to here");
		eventTypeDropdown.setAdapter(eventTypeAdapter);

		eventYearDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
				String eventNameKey = eventTypeDropdown.getSelectedItem().toString().toLowerCase();
				if (eventNameKey.equalsIgnoreCase("Select Event Type")) {
					eventNameKey = "default";
				} else
					eventNameKey = eventNameKey + "Dropdown" + eventYearDropdown.getSelectedItem().toString();

				int eventNameId = eventIds.get(eventNameKey);
				String[] eventNames = getResources().getStringArray(eventNameId);
				ArrayAdapter<String> eventNameAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_style, eventNames);
				eventNameDropdown.setAdapter(eventNameAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		eventTypeDropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
				String eventNameKey = eventTypeDropdown.getSelectedItem().toString().toLowerCase();
				if (eventNameKey.equalsIgnoreCase("Select Event Type")) {
					eventNameKey = "default";
				} else
					eventNameKey = eventNameKey + "Dropdown" + eventYearDropdown.getSelectedItem().toString();

				int eventNameId = eventIds.get(eventNameKey);
				String[] eventNames = getResources().getStringArray(eventNameId);
				ArrayAdapter<String> eventNameAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_style, eventNames);
				eventNameDropdown.setAdapter(eventNameAdapter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		Button dialogButton = (Button) dialog.findViewById(R.id.editEventDetailsSubmitButton);
		dialogButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				eventYearEditTextView = (EditText) findViewById(R.id.eventYearEditTextView);
				eventYearEditTextView.setText(eventYearDropdown.getSelectedItem().toString());

				eventNameEditTextView = (EditText) findViewById(R.id.eventNameEditTextView);
				eventNameEditTextView.setText(eventNameDropdown.getSelectedItem().toString());

				dialog.dismiss();
			}
		});
		dialog.show();
	}
}
